﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PapnKukn.Silverlight.SilverKnight.ScoreService;
using System.ServiceModel;

namespace PapnKukn.Silverlight.SilverKnight
{
    public partial class Modal : UserControl
    {
        #region Declarations
        private int score = 0;
        #endregion

        #region Properties
        public string Message
        {
            get
            {
                return lblMessage.Text;
            }
            set
            {
                lblMessage.Text = value;
            }
        }

        public int Score
        {
            set
            {
                this.score = value;
                lblScore.Text = "Score: " + value;
            }
        }
        #endregion

        #region Constructor
        public Modal()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods
        public void Show()
        {
            this.Visibility = Visibility.Visible;
        }

        public void Hide()
        {
            this.Visibility = Visibility.Collapsed;
        }
        #endregion

        #region Events
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (AppConfig.AppHostBase != null)
                {
                    BasicHttpBinding binding = new BasicHttpBinding();
                    EndpointAddress address = new EndpointAddress(AppConfig.ServiceUrl);
                    ScoreServicePortTypeClient client = new ScoreServicePortTypeClient(binding, address);
                    client.AddScoreCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_AddScoreCompleted);
                    client.AddScoreAsync(txtName.Text, "" + this.score);
                }
                else
                {
                    ScoreServicePortTypeClient client = new ScoreServicePortTypeClient();
                    client.AddScoreCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_AddScoreCompleted);
                    client.AddScoreAsync(txtName.Text, "" + this.score);
                }
            }
            catch (Exception ex)
            {
                Logger.ShowException(ex);
            }

            Hide();
        }

        protected void client_AddScoreCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                OnRefreshScoreRequest(new RoutedEventArgs());
            }
        }
        #endregion

        #region Bubble events
        public event RoutedEventHandler RefreshScoreRequest;
        protected virtual void OnRefreshScoreRequest(RoutedEventArgs e)
        {
            if (RefreshScoreRequest != null)
            {
                RefreshScoreRequest(this, e);
            }
        }
        #endregion
    }
}
